﻿using System;
using System.Collections.Generic;
using System.Web.UI;

namespace AutoCompleteTextBox
{
    [TargetControlType(typeof(Control))]
    public class AutoCompleteTextBoxExtender : ExtenderControl
    {
        private string SourceID { get; set; }

        public string DataSourceId { get; set; }

        private AutocompleteDataSource AutoCompleteDataSource { get; set; }

        public static string YUIPath { get; set; }

        static AutoCompleteTextBoxExtender()
        {
            YUIPath = ControlHelper.DefaultYUIPath;
        }

        protected override IEnumerable<ScriptDescriptor> GetScriptDescriptors(Control targetControl)
        {
            var control = new ScriptControlDescriptor("AutoCompleteTextBox.AutoCompleteTextBox", targetControl.ClientID);
            control.AddProperty("SourceID", SourceID);
            yield return control;
        }

        protected override IEnumerable<ScriptReference> GetScriptReferences()
        {
            return new ScriptReference[]
            {
                 new AutoCompleteTextBoxScript()
            };
        }

        protected override void OnPreRender(EventArgs e)
        {
            YUIPathManager.YUIPathEditor();
            AutoCompleteTextBoxDataSourceManager dataSourceManager = new AutoCompleteTextBoxDataSourceManager(AutoCompleteDataSource, this, DataSourceId, YUIPath);
            SourceID = dataSourceManager.SourceID;
            base.OnPreRender(e);
        }
    }
}
